﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D2D2.SharedInterfaces.Networking;
using D2D2.SharedInterfaces.SharedObject;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace D2D2.Core
{
    public class Chest : BattleSprite
    {
        private List<Drop> _drops = new List<Drop>();
        private bool _isOffset;

        private const int ACT_VALUE = 30;
        private readonly List<ChestObject> _snapshots = new List<ChestObject>();

        public Chest(Rectangle location, ChestObject chestObject)
            : base(location, SpriteType.Chest)
        {
            LiveRating = 0;
            Id = chestObject.Id;

            //ChestObject = chestObject;

            _texture = _chestTexture;
            _progress.ProgressType = ProgressType.Gold;
            _progress.SetProgress(chestObject.TotalVolume);
        }

        //public ChestObject ChestObject { get; set; }
        public long Id { get; set; }

        public void AddSnapshot(ChestObject monster)
        {
            monster.LocalTime = NetTime.Now;
            _snapshots.Add(monster);

            if (_snapshots.Count > 10)
                _snapshots.RemoveAt(0);
        }

        private ChestObject GetInerpolatedChest()
        {
            double shiftTime = NetTime.Now - GlobalConst.INTERPOLATION_SHIFT;

            ChestObject before = null;
            ChestObject after = null;
            for (int i = _snapshots.Count - 1; i >= 0; i--)
            {
                if (_snapshots[i].LocalTime < shiftTime)
                {
                    before = _snapshots[i];
                    break;
                }

                if (i == 0)
                {
                    after = _snapshots[i];
                    break;
                }

                if (_snapshots[i].LocalTime >= shiftTime && _snapshots[i - 1].LocalTime <= shiftTime)
                {
                    before = _snapshots[i - 1];
                    after = _snapshots[i];
                    break;
                }
            }

            ChestObject chest = null;

            if (before == null) chest = after;
            if (after == null) chest = before;

            if (before != null && after != null)
            {
                double weight = (shiftTime - before.LocalTime) / (after.LocalTime - before.LocalTime);
                chest = before.Clone();
                chest.CurrentVolume = MathHelper.Lerp(before.CurrentVolume, after.CurrentVolume, (float)weight);
            }

            return chest;
        }

        public override void Update(GameTime gameTime)
        {
            if (_snapshots.Count == 0) return;

            ChestObject chest = GetInerpolatedChest();

            Alive = chest.CurrentVolume > 0;

            _progress.SetProgress(chest.CurrentVolume, chest.TotalVolume);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, Location, _initialFrame, Color.White);

            _progress.Draw(spriteBatch);

            //_drops.ForEach(d => d.Draw(spriteBatch));
        }

        public override void DecreaseHit()
        {


        }

        protected override int GetFrameCount()
        {
            return 1;
        }
    }
}
